//
//  GQHMobileView.m
//  Xanthium
//
//  Created by GuanQinghao on 02/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHMobileView.h"
#import "GQHHeader.h"

@interface GQHMobileView () <UITableViewDelegate,UITableViewDataSource> {
    
    UIButton *codeButton;
}

@end


@implementation GQHMobileView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

#pragma mark --Data
- (void)loadLocalData {
    
}

#pragma mark --View
- (void)loadUserInterface {
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = kWhiteColor;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.scrollEnabled = NO;
    if (@available(iOS 11.0, *)) {
        
        tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.qh_statusBarHeight + self.qh_navigationBarHeight);
        make.left.and.right.and.bottom.mas_equalTo(self);
    }];
}

#pragma mark --Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (0 == indexPath.row) {
        
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.selectionStyle = UITableViewCellSeparatorStyleNone;
       
        UILabel *mobileCaptionLabel = [[UILabel alloc] init];
        mobileCaptionLabel.font = kBiggerFont;
        mobileCaptionLabel.text = @"手机号";
        mobileCaptionLabel.textColor = kDarkGrayColor;
        mobileCaptionLabel.textAlignment = NSTextAlignmentLeft;
        [cell.contentView addSubview:mobileCaptionLabel];
        [mobileCaptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.top.and.bottom.mas_equalTo(cell.contentView);
            make.left.mas_equalTo(cell.contentView).with.offset(GQH_MARGIN_SINGLE);
            make.width.mas_equalTo(60);
        }];
        
        UIView *mobileContainerView = [[UIView alloc] init];
        mobileContainerView.backgroundColor = kAppleGrayColor;
        mobileContainerView.layer.cornerRadius = 5.0;
        mobileContainerView.layer.masksToBounds = YES;
        [cell.contentView addSubview:mobileContainerView];
        [mobileContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(cell.contentView).with.offset(5);
            make.bottom.mas_equalTo(cell.contentView).with.inset(5);
            make.left.mas_equalTo(mobileCaptionLabel.mas_right);
            make.right.mas_equalTo(cell.contentView).with.inset(GQH_MARGIN_SINGLE);
        }];
        
        _qh_mobileTextField = [[UITextField alloc] init];
        _qh_mobileTextField.backgroundColor = kClearColor;
        _qh_mobileTextField.font = kBiggerFont;
        _qh_mobileTextField.textColor = kDarkBlackColor;
        _qh_mobileTextField.placeholder = @"手机号";
        _qh_mobileTextField.textAlignment = NSTextAlignmentLeft;
        _qh_mobileTextField.keyboardType = UIKeyboardTypeNumberPad;
        [mobileContainerView addSubview:_qh_mobileTextField];
        [_qh_mobileTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(mobileContainerView).with.offset(5);
            make.bottom.mas_equalTo(mobileContainerView).with.inset(5);
            make.left.mas_equalTo(mobileContainerView).with.offset(10);
            make.right.mas_equalTo(mobileContainerView).with.inset(5);
        }];
        
        return cell;
    } else {
        
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.selectionStyle = UITableViewCellSeparatorStyleNone;
        
        UILabel *codeCaptionLabel = [[UILabel alloc] init];
        codeCaptionLabel.font = kBiggerFont;
        codeCaptionLabel.text = @"验证码";
        codeCaptionLabel.textColor = kDarkGrayColor;
        codeCaptionLabel.textAlignment = NSTextAlignmentLeft;
        [cell.contentView addSubview:codeCaptionLabel];
        [codeCaptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.and.bottom.mas_equalTo(cell.contentView);
            make.left.mas_equalTo(cell.contentView).with.offset(GQH_MARGIN_SINGLE);
            make.width.mas_equalTo(60);
        }];
        
        codeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        codeButton.backgroundColor = kAppGreenColor;
        codeButton.layer.cornerRadius = 5.0;
        codeButton.layer.masksToBounds = YES;
        [codeButton setTitle:@"验证码" forState:UIControlStateNormal];
        [codeButton setTitleColor:kAppRedColor forState:UIControlStateNormal];
        [codeButton addTarget:self action:@selector(startCountingDown:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:codeButton];
        [codeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(cell.contentView).with.offset(5);
            make.bottom.mas_equalTo(cell.contentView).with.inset(5);
            make.right.mas_equalTo(cell.contentView).with.inset(20);
            make.width.mas_equalTo(120);
        }];
        
        UIView *codeContainerView = [[UIView alloc] init];
        codeContainerView.backgroundColor = kAppleGrayColor;
        codeContainerView.layer.cornerRadius = 5.0;
        codeContainerView.layer.masksToBounds = YES;
        [cell addSubview:codeContainerView];
        [codeContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(cell.contentView).with.offset(5);
            make.bottom.mas_equalTo(cell.contentView).with.inset(5);
            make.left.mas_equalTo(codeCaptionLabel.mas_right);
            make.right.mas_equalTo(codeButton.mas_left).with.inset(10);
        }];
        
        _qh_codeTextField = [[UITextField alloc] init];
        _qh_codeTextField.backgroundColor = kClearColor;
        _qh_codeTextField.font = kBiggerFont;
        _qh_codeTextField.textColor = kDarkBlackColor;
        _qh_codeTextField.placeholder = @"验证码";
        _qh_codeTextField.textAlignment = NSTextAlignmentLeft;
        _qh_codeTextField.keyboardType = UIKeyboardTypeNumberPad;
        [codeContainerView addSubview:_qh_codeTextField];
        [_qh_codeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(codeContainerView).with.offset(5);
            make.bottom.mas_equalTo(codeContainerView).with.inset(5);
            make.left.mas_equalTo(codeContainerView).with.offset(10);
            make.right.mas_equalTo(codeContainerView).with.inset(5);
        }];
        
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 10.0f;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 120.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    UIView *footerView = [[UIView alloc] init];
    footerView.backgroundColor = kWhiteColor;
    
    UIView *separatorView = [[UIView alloc] init];
    separatorView.backgroundColor = kLightGrayColor;
    [footerView addSubview:separatorView];
    [separatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.and.right.mas_equalTo(footerView).with.inset(GQH_MARGIN_SINGLE);
        make.top.mas_equalTo(footerView).with.offset(10);
        make.height.mas_equalTo(1);
    }];
    
    UILabel *captionLabel = [[UILabel alloc] init];
    captionLabel.font = [UIFont systemFontOfSize:16 weight:1.1];
    captionLabel.text = @"温馨提示";
    captionLabel.textColor = kBlackColor;
    captionLabel.textAlignment = NSTextAlignmentLeft;
    [footerView addSubview:captionLabel];
    [captionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(footerView).with.offset(GQH_MARGIN_SINGLE);
        make.right.mas_equalTo(footerView).with.inset(GQH_MARGIN_SINGLE);
        make.top.mas_equalTo(footerView).with.offset(15);
        make.height.mas_equalTo(30);
    }];
    
    UILabel *descriptionLabel = [[UILabel alloc] init];
    descriptionLabel.font = kDefaultFont;
    descriptionLabel.text = @"如果无法接收短信验证码，请致电联系平台客服。\n客服电话: 029-8888888";
    descriptionLabel.textColor = kDarkGrayColor;
    descriptionLabel.textAlignment = NSTextAlignmentLeft;
    descriptionLabel.numberOfLines = 0;
    [footerView addSubview:descriptionLabel];
    [descriptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.and.right.mas_equalTo(footerView).with.inset(GQH_MARGIN_SINGLE);
        make.top.mas_equalTo(captionLabel.mas_bottom);
        make.height.mas_equalTo(60);
    }];
    
    return footerView;
}

// 验证码倒计时
- (IBAction)startCountingDown:(UIButton *)sender {
    DLog(@"验证码倒计时");
    
    NSString *mobile = _qh_mobileTextField.text;
    if ([_qh_delegate respondsToSelector:@selector(qh_requestCodeWithMobile:)]) {
        
        [_qh_delegate qh_requestCodeWithMobile:mobile];
        
        __block NSInteger time = 59;
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
        dispatch_source_set_timer(timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0);
        dispatch_source_set_event_handler(timer, ^{
            if (time <= 0) {
                dispatch_source_cancel(timer);
                dispatch_async(dispatch_get_main_queue(), ^{
                    [codeButton setTitle:@"重新发送" forState:UIControlStateNormal];
                    [codeButton setTitleColor:kAppRedColor forState:UIControlStateNormal];
                    codeButton.userInteractionEnabled = YES;
                    _qh_mobileTextField.userInteractionEnabled = YES;
                });
            } else {
                int seconds = time % 60;
                dispatch_async(dispatch_get_main_queue(), ^{
                    [codeButton setTitle:[NSString stringWithFormat:@"重新发送(%.2d)", seconds] forState:UIControlStateNormal];
                    [codeButton setTitleColor:kAppRedColor forState:UIControlStateNormal];
                    codeButton.userInteractionEnabled = NO;
                    _qh_mobileTextField.userInteractionEnabled = NO;
                });
                time--;
            }
        });
        dispatch_resume(timer);
    }
}

@end
